<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="dxlateplate.LatePlate" %>
<%@ page import="dxlateplate.PMF" %>

<html>
<head>
    <link rel="icon" type="image/png" href="/favicon.png">
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
    <title>Delta Chi Late Plates</title>
</head>
<body>
	<div id="crest">
        <img alt="crest" src="images/crest.png" height="85" width="50">
        <a href="latePlateHome.jsp"><img alt="Late Plates" src="images/late_plate_title.png"></a>
        <img alt="crest" src="images/crest.png" height="85" width="50">
    </div>
    <div align="center">
    <table>
		<tr>
			<td valign="top">
				<%
					UserService userService = UserServiceFactory.getUserService();
		            User user = userService.getCurrentUser();
		            PersistenceManager pm = PMF.get().getPersistenceManager();
		            
		            //non permanent
		            String query = "select from " + LatePlate.class.getName() + " where permanent==false order by value asc, type asc";
		            List<LatePlate> latePlateList = (List<LatePlate>) pm.newQuery(query).execute();
		            
		            //permanent
		            String query2 = "select from " + LatePlate.class.getName() + " where permanent==true order by value asc, type asc";
		            List<LatePlate> permList = (List<LatePlate>) pm.newQuery(query2).execute();
		            
		            if (latePlateList.isEmpty()) {
		        %>
		        <div align="center" class="plateTableTitle"><h1>No Current Late Plates.</h1></div>
		        <%
		            } else {
		        %>
		        <div align="center" class="plateTableTitle">
		        <h1>Current Late Plates</h1>
		        <table border="1" class="plateTable">
		            <tr>
		                <td><b>Name</b></td>
		                <td><b>Day</b></td>
		                <td><b>Meal</b></td>
		                <td><b>Comments</b></td>
		                <td><b>Time Requested</b></td>
		                <td><b>Delete</b></td>
		            </tr>
		        <%
		                for (LatePlate plate : latePlateList) {
		        %>
		                    <tr>
		        <%
		                    //Requester's Name
		                    if (plate.getName() == null) {
		        %>
		                        <td>Somebody</td>
		        <%
		                    } else {
		        %>
		                        <td><%= plate.getName() %></td>
		                        <td><%= plate.getDay() %></td>
		        <%
		                    }
		                    //Lunch or Dinner
		                    if (plate.getType() == 1) {
		        %>
		                        <td>Lunch</td>
		        <%
		                    } else {
		        %>
		                        <td>Dinner</td>
		        <%
		                    }
		        %>
		                        <td><%= plate.getComments() %></td>
		                        <td><%= plate.getReqTime() %></td>
		
		        <%
		                    if ((plate.getUserName() != null) && user != null) {
		                        if (plate.getUserName().equals(user.getEmail())) {
		            %>
		                            <td align="center"><br><form action="/deleteLatePlate" method="post">
		                                    <input type="hidden" name="plateKey" value=<%= KeyFactory.keyToString(plate.getKey()) %> />
		                                    <input type="image" src="images/remove.png" height="24" width="24"  border="0" alt="Delete" />
		                            </form></td>
		            <%
		                        } else {
		            %>
		                            <td></td>
		                        </tr>
		            <%
		                        }
		            %>
		            <%        } else {
		            %>
		                            <td></td>
		                        </tr>
		            <%
		                    }
		            %>
		            <%
		                }
		            }
		        %>
		        </table>
		        </div>
			</td>	
			<td valign="top">
				<%
		            if (permList.isEmpty()) {
		        %>
		        <div align="center" class="plateTableTitle"><h1>No Permanent Late Plates.</h1></div>
		        <%
		            } else {
		        %>
		        <div align="center" class="plateTableTitle">
		        <h1>Permanent Late Plates</h1>
		        <table border="1" class="plateTable">
		            <tr>
		                <td><b>Name</b></td>
		                <td><b>Day</b></td>
		                <td><b>Meal</b></td>
		                <td><b>Comments</b></td>
		                <td><b>Time Requested</b></td>
		                <td><b>Delete</b></td>
		            </tr>
		        <%
		                for (LatePlate plate : permList) {
		        %>
		                    <tr>
		        <%
		                    //Requester's Name
		                    if (plate.getName() == null) {
		        %>
		                        <td>Somebody</td>
		        <%
		                    } else {
		        %>
		                        <td><%= plate.getName() %></td>
		                        <td><%= plate.getDay() %></td>
		        <%
		                    }
		                    //Lunch or Dinner
		                    if (plate.getType() == 1) {
		        %>
		                        <td>Lunch</td>
		        <%
		                    } else {
		        %>
		                        <td>Dinner</td>
		        <%
		                    }
		        %>
		                        <td><%= plate.getComments() %></td>
		                        <td><%= plate.getReqTime() %></td>
		
		        <%
		                    if ((plate.getUserName() != null) && user != null) {
		                        if (plate.getUserName().equals(user.getEmail())) {
		            %>
		                            <td align="center"><br><form action="/deleteLatePlate" method="post">
		                                    <input type="hidden" name="plateKey" value=<%= KeyFactory.keyToString(plate.getKey()) %> />
		                                    <input type="image" src="images/remove.png" height="24" width="24"  border="0" alt="Delete" />
		                            </form></td>
		            <%
		                        } else {
		            %>
		                            <td></td>
		                        </tr>
		            <%
		                        }
		            %>
		            <%        } else {
		            %>
		                            <td></td>
		                        </tr>
		            <%
		                    }
		            %>
		            <%
		                }
		            }
		        %>
		        </table>
		        </div>
			</td>
		</tr>
	</table>
	<p class="white">&copy Travis Wooten 2011-2012</p>
	</div>
</body>
</html>